<?php
/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace common\traits;

use common\model\base\BaseModel;
use common\service\BaseService;
use common\validate\BaseValidate;
use think\response\Json;

/**
 * Trait ControllerTrait
 * @package common\traits
 * @property BaseModel $model
 */
trait ControllerTrait
{
    /**
     * @var BaseValidate
     */
    public $validate;
    /**
     * @var BaseService
     */
    public $service;

    /**
     * @return Json
     */
    public function index()
    {
        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $list = $this->service->getList($param);
        $count = $this->service->getCount($param);
        return $this->json_responce( compact(['list', 'count']));
    }

    /**
     * @param $id
     * @return Json
     */
    public function read()
    {
        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->service->getByID($param);
        return $this->json_responce($data);
    }

    /**
     * @return Json
     */
    public function save()
    {
        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->service->createData($param);
        return $this->json_responce();
    }

    /**
     * @return Json
     */
    public function update()
    {
        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->service->updateByID($param);
        return $this->json_responce();
    }

    /**
     * @return Json
     */
    public function delete()
    {
        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->service->delByID($param);
        return $this->json_responce();
    }

    /**
     * @return Json
     */
    public function deletes()
    {
        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->service->delByIDs($param);
        return $this->json_responce();
    }

    /**
     * @return Json
     */
    public function enables()
    {
        $param = $this->param;
        $this->validate->runCheck($param, '', __FUNCTION__);
        $data = $this->service->enableByIDs($param);
        return $this->json_responce();
    }
}
